package com.xxxx.crm.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.crm.base.BaseService;
import com.xxxx.crm.dao.DataDicMapper;
import com.xxxx.crm.query.DataDicQuery;
import com.xxxx.crm.utils.AssertUtil;
import com.xxxx.crm.utils.PhoneUtil;
import com.xxxx.crm.vo.DataDic;
import com.xxxx.crm.vo.DataDic;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class DataDicService extends BaseService<DataDic,Integer> {
    @Resource
    private DataDicMapper dataDicMapper;

    public Map<String, Object> queryDataDicByParams(DataDicQuery dataDicQuery) {
        Map<String, Object> map = new HashMap<>();
        PageHelper.startPage(dataDicQuery.getPage(), dataDicQuery.getLimit());
        PageInfo<DataDic> pageInfo = new PageInfo<>(dataDicMapper.selectByParams(dataDicQuery));
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;

    }

    /***
     * 添加字典数据
     *  1. 参数校验
     *      字典ID   非空，数据存在
     *      字典类型   非空
     *      字典值     非空
     *  2. 设置参数的默认值
     *      是否有效    默认有效
     *      创建时间    系统当前时间
     *      修改时间    系统当前时间
     *  3. 执行添加操作，判断受影响的行数
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void addDataDic(DataDic dataDic) {
        checkDataDic(dataDic);

        dataDic.setIsValid((byte) 1);
        dataDic.setCreateDate(new Date());
        dataDic.setUpdateDate(new Date());

        AssertUtil.isTrue(insertSelective(dataDic) != 1, "字典信息添加失败！");
    }

    /**
     *  参数校验
     *      字典ID   非空，数据存在
     *      字典类型   非空
     *      字典值     非空
     */
    private void checkDataDic(DataDic dataDic) {
        AssertUtil.isTrue(StringUtils.isBlank(dataDic.getDataDicName()), "字典类型不能为空！");
        AssertUtil.isTrue(StringUtils.isBlank(dataDic.getDataDicValue()), "字典值不能为空！");
    }


    /**
     * 更新字典数据
     *  1. 参数校验
     *      字典ID   非空，数据存在
     *      字典类型   非空
     *      字典值     非空
     *  2. 设置参数的默认值
     *      修改时间    系统当前时间
     *  3. 执行更新操作，判断受影响的行数
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void updateDataDic(DataDic dataDic){

        AssertUtil.isTrue(null == dataDic.getId() || selectByPrimaryKey(dataDic.getId()) == null, "待更新的记录不存在！");
        checkDataDic(dataDic);
        dataDic.setUpdateDate(new Date());
        AssertUtil.isTrue(updateByPrimaryKeySelective(dataDic) != 1, "字典信息更新失败！");
    }

    /**
     * 删除字典（多）
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public void deleteDataDic(Integer[] ids) {
        // 判断ID是否为空
        AssertUtil.isTrue(null == ids || ids.length < 1, "待删除记录不存在！");
        // 执行删除（更新）操作，判断受影响的行数
        AssertUtil.isTrue(dataDicMapper.deleteBatch(ids) != ids.length, "字典数据删除失败！");
    }
}
